<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2018/3/22
 * Time: 9:24
 */

namespace app\api\model;
use app\common\lib\exception\ApiException;
use think\Model;
use think\Db;
use think\Log;

class Account extends Model
{

    /**
     * 查询用户全部流水记录列表
     * @param id int 用户id *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return \think\response\Json
     */
    public function index(){
        $data = $_GET;
        if (empty($data)) {
            throw new ApiException('数据不合法', 404);
        }
        //id int 用户id 必传
        if (empty($data['id'])) {
            throw new ApiException('id is not find', 404);
        }
        try {
            $info =Db::name('account')
                ->order('record_time desc')
                ->where(array('user_id'=>$data['id']))
                ->paginate(6);

            return show(config('code.success'), '获取数据成功', $info);

        } catch (\Exception $e) {
            Log::write($e->getMessage());
            throw new ApiException(request()->controller().'/'.request()->action().' '.$e->getMessage(), 400);
        }
    }

    /**
     * 查询用户学币流水记录列表
     * @param id int 用户id *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return \think\response\Json
     */
    public function coin_record(){
        $data = $_POST;
        if (empty($data)) {
            throw new ApiException('数据不合法', 404);
        }
        //id int 用户id 必传
        if (empty($data['id'])) {
            throw new ApiException('id is not find', 404);
        }
        try {
            $info =Db::name('account')
                ->order('record_time desc')
                ->where('user_id='.$data['id'].' AND cost_type=1')
                ->paginate(6);

            return show(config('code.success'), '获取数据成功', $info);

        } catch (\Exception $e) {
            Log::write($e->getMessage());
            throw new ApiException(request()->controller().'/'.request()->action().' '.$e->getMessage(), 400);
        }
    }

    /**
     * 查询用户学费流水记录列表
     * @param id int 用户id *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return \think\response\Json
     */
    public function tuition_record(){
        $data = $_POST;
        if (empty($data)) {
            throw new ApiException('数据不合法', 404);
        }
        //id int 用户id 必传
        if (empty($data['id'])) {
            throw new ApiException('id is not find', 404);
        }
        try {
            $info =Db::name('account')
                ->order('record_time desc')
                ->where('user_id='.$data['id'].' AND cost_type=2')
                ->paginate(6);

            return show(config('code.success'), '获取数据成功', $info);

        } catch (\Exception $e) {
            Log::write($e->getMessage());
            throw new ApiException(request()->controller().'/'.request()->action().' '.$e->getMessage(), 400);
        }
    }

    /**
     * 查询用户奖金流水记录列表
     * @param id int 用户id *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return \think\response\Json
     */
    public function bonus_record(){
        $data = $_POST;
        if (empty($data)) {
            throw new ApiException('数据不合法', 404);
        }
        //id int 用户id 必传
        if (empty($data['id'])) {
            throw new ApiException('id is not find', 404);
        }
        try {
            $info =Db::name('account')
                ->order('record_time desc')
                ->where('user_id='.$data['id'].' AND cost_type!=1 AND cost_type!=2')
                ->paginate(6);

            return show(config('code.success'), '获取数据成功', $info);

        } catch (\Exception $e) {
            Log::write($e->getMessage());
            throw new ApiException(request()->controller().'/'.request()->action().' '.$e->getMessage(), 400);
        }
    }

    /**
     * 查询用户奖金提现流水记录列表
     * @param id int 用户id *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return \think\response\Json
     */
    public function bonus_withdrawals_record(){
        $data = $_POST;
        if (empty($data)) {
            throw new ApiException('数据不合法', 404);
        }
        //id int 用户id 必传
        if (empty($data['id'])) {
            throw new ApiException('id is not find', 404);
        }
        try {
            $info =Db::name('account')
                ->order('record_time desc')
                ->where([
                    'user_id' => $data['id'],
                    'account_type' => 2,
                    'channel' => 'wx',
                    'cost_type' => 4
                ])
                ->paginate(6);

            return show(config('code.success'), '获取数据成功', $info);

        } catch (\Exception $e) {
            Log::write($e->getMessage());
            throw new ApiException(request()->controller().'/'.request()->action().' '.$e->getMessage(), 400);
        }
    }

    /**
     * 查询学生学费提现流水记录列表
     * @param id int 用户id *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return \think\response\Json
     */
    public function tuition_withdrawals_record(){
        $data = $_POST;
        if (empty($data)) {
            throw new ApiException('数据不合法', 404);
        }
        //id int 用户id 必传
        if (empty($data['id'])) {
            throw new ApiException('id is not find', 404);
        }
        try {
            $info =Db::name('account')
                ->order('record_time desc')
                ->where([
                    'user_id' => $data['id'],
                    'account_type' => 2,
                    'channel' => 'wx',
                    'cost_type' => 2
                ])
                ->paginate(6);

            return show(config('code.success'), '获取数据成功', $info);

        } catch (\Exception $e) {
            Log::write($e->getMessage());
            throw new ApiException(request()->controller().'/'.request()->action().' '.$e->getMessage(), 400);
        }
    }

    /**
     * 查询教师费用提现流水记录列表
     * @param id int 用户id *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return \think\response\Json
     */
    public function teacher_cost_withdrawals_record(){
        $data = $_POST;
        if (empty($data)) {
            throw new ApiException('数据不合法', 404);
        }
        //id int 用户id 必传
        if (empty($data['id'])) {
            throw new ApiException('id is not find', 404);
        }
        try {
            $info =Db::name('account')
                ->order('record_time desc')
                ->where([
                    'user_id' => $data['id'],
                    'account_type' => 2,
                    'channel' => 'wx',
                    'cost_type' => 3
                ])
                ->paginate(6);

            return show(config('code.success'), '获取数据成功', $info);

        } catch (\Exception $e) {
            Log::write($e->getMessage());
            throw new ApiException(request()->controller().'/'.request()->action().' '.$e->getMessage(), 400);
        }
    }

    /**
     * 查询用户奖金流水记录列表
     * @param id int 用户id *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return \think\response\Json
     */
    public function teacher_cost_record(){
        $data = $_POST;
        if (empty($data)) {
            throw new ApiException('数据不合法', 404);
        }
        //id int 用户id 必传
        if (empty($data['id'])) {
            throw new ApiException('id is not find', 404);
        }
        try {
            $info =Db::name('account')
                ->order('record_time desc')
                ->where('user_id='.$data['id'].' AND cost_type=3')
                ->paginate(6);

            return show(config('code.success'), '获取数据成功', $info);

        } catch (\Exception $e) {
            Log::write($e->getMessage());
            throw new ApiException(request()->controller().'/'.request()->action().' '.$e->getMessage(), 400);
        }
    }

    /**
     * 添加流水记录
     * @param string $account_sn 流水编号 *必传
     * @param string $user_id 用户id *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return void
     */
    public static function addAccount($account_sn,$user_id,$cost_description,$cost,$cost_type,$channel,$account_type = 3,$status = 1){

        $account = [
            //流水编号
            'account_sn' => $account_sn,

            //流水用户id
            'user_id' => $user_id,

            //费用描述
            'cost_description' => $cost_description,

            //费用
            'cost' => $cost,

            //流水时间
            'record_time' => time(),

            //费用类型 1学币 2学费 3教师费用 4推荐奖金 5管理费用 6专项费用 7教具费用 8合作费用 9教务费用 10教室费用 11重上费用
            'cost_type' => $cost_type,

            //支付渠道 backstage（后台） ali（支付宝）wx（微信）remain（余额）
            'channel' => $channel,

            //状态 0失败 1成功 2其他
            'status' => $status,

            //流水类型 1充值 2提现 3其他 默认3
            'account_type' => $account_type,
        ];

        model('account')->insert($account);
    }
}